package com.dhu.demo.controller;

import com.dhu.demo.JWT.JWTEntity;
import com.dhu.demo.JWT.PassToken;
import com.dhu.demo.entity.*;
import com.dhu.demo.service.ScService;
import com.dhu.demo.service.StudentService;
import com.dhu.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RequestMapping("/user")
@RestController
@CrossOrigin("*")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private ScService scService;
    @Autowired
    private StudentService studentService;

    @RequestMapping("/findAllUser")
    public List<User> getUser(){
        return userService.findAll();
    }

    @PassToken
    @RequestMapping("/register")
    private String register(@RequestBody User record){
        record.setLevel("s");
        return userService.insert(record);
    }

    @RequestMapping("/updatePasswordByAccount")
    private int updatePasswordByAccount(@RequestBody User user){
        String account=user.getAccount();
        String password=user.getPassword();
        String level=null;
        User record=new User(account,password,level);
        return userService.updatePasswordByAccount(record);
    }

    @RequestMapping("/updateLevelByAccount")
    private int updateLevelByAccount(@RequestBody User user){
        System.out.println(user.getLevelname());
        String account=user.getAccount();
        String level;
        if (user.getLevelname().equals("管理员"))
            level = "a";
        else
            level = "s";
        String password=null;
        User record=new User(account,password,level);
        return userService.updateLevelByAccount(record);
    }

    @RequestMapping("/deleteUser")
    private int deleteInfo(@RequestBody User user){
        String account=user.getAccount();
        return userService.deleteByPrimaryKey(account);
    }

    @ResponseBody
    @PassToken
    @PostMapping("/judgeLogin")
    public String judgeLogin(@RequestBody User user) {
        //返回s=学生，a=管理，其余查询失败
        String account=user.getAccount();
        String password=user.getPassword();
        String level = userService.judgeLogin(account,password);
        if(level.equals("查询失败")){
            return "查询失败";
        }
        String token = JWTEntity.createToken(account,password,level);
        return token;
    }

    @RequestMapping("testToken")
    public String testToken(HttpServletRequest request){
        return (String)request.getAttribute("level");
    }

    @RequestMapping("findMaxScore")
    public List<StudentWithScore> findMaxScore(@RequestBody Sc sc){
        String classname=sc.getClassname();
        int max=scService.selectAllInfoByClassName(classname).stream().mapToInt(Sc::getScore).max().getAsInt();
        return studentService.selectAllInfoAndScoreByScore(max);
    }

    @RequestMapping("findMinScore")
    public List<StudentWithScore> findMinScore(@RequestBody Sc sc){
        String classname=sc.getClassname();
        int min=scService.selectAllInfoByClassName(classname).stream().mapToInt(Sc::getScore).min().getAsInt();
        return studentService.selectAllInfoAndScoreByScore(min);
    }

    @RequestMapping("findAveScore")
    public double findAveScore(@RequestBody Sc sc){
        String classname=sc.getClassname();
        Double request = scService.selectAllInfoByClassName(classname).stream().mapToInt(Sc::getScore).average().getAsDouble();
        request = Double.valueOf(String.format("%.1f", request ));
        return request;
    }

    @RequestMapping("findLossScore")
    public List<StudentWithScore> findLossScore(@RequestBody Sc sc){
        String classname=sc.getClassname();
        String grade="E";
        StudentWithScore loss = new StudentWithScore(classname,grade);
        return studentService.selectAllInfoAndScoreByClassnameAndGrade(loss);
    }

    @RequestMapping("/getGradeAsMap")
    public Map<String,Integer> getGradeAsMap(@RequestBody Sc sc){
        String classname=sc.getClassname();
        Map map = new HashMap();
        String[] strings={"A","B","C","D","E"};
        for(String grade:strings){
            StudentWithScore loss = new StudentWithScore(classname,grade);
            int sum=  studentService.selectAllInfoAndScoreByClassnameAndGrade(loss).size();
            map.put(grade,sum);
        }
        return map;
    }

    @RequestMapping("findGradeScore")
    public List<StudentWithScore> findGradeScore(@RequestBody Sc sc){
        String classname=sc.getClassname();
        String grade=sc.getGrade();
        StudentWithScore loss = new StudentWithScore(classname,grade);
        return studentService.selectAllInfoAndScoreByClassnameAndGrade(loss);
    }

    @RequestMapping("/findScoreMap")
    public List<StudentWithScore> findScore(@RequestBody Student student)
    {
        String classnumber=student.getClassnumber();
        String department=student.getDepartment();
        String major=student.getMajor();
        if(major.equals(""))
        {
            return studentService.selectScoreMapByDepartment(department);
        }
        else if(classnumber.equals(""))
        {
            return studentService.selectScoreMapByMajor(department,major);
        }
        else
        {
            return studentService.selectScoreMapByClassNumber(department,major,classnumber);
        }
    }

    @RequestMapping("/findScoreMapByClassName")
    public List<StudentWithScore> findScoreMapByClassName(@RequestBody Sc sc)
    {
        String classname=sc.getClassname();
        return studentService.selectScoreMapByClassName(classname);
    }

    @RequestMapping("/fileimport")
    public String excelfileimport(@RequestParam("file") MultipartFile multipartFile){
        scService.importFile(multipartFile);
        return "success";
    }

   /* @RequestMapping("/sqlfileimport")
    public String sqlfileimport(@RequestParam("file") MultipartFile multipartFile){
        String msg=scService.importSql(multipartFile);
        return msg;
    }*/
}
